import sys
sys.path.append("..")
import Cust.leancloudapi
GetApi = Cust.leancloudapi.GetApi()
teacherm = GetApi.getTeacher()
class Teacher:
    def getAllTeacher(self):
      query = teacherm.query
      query.limit(1000)
      s = query.find()
      teacher_list = []
      for item in s:
           teacher_id = item.id
           teacher_name = item.get('TeacherName')
           teacher_no = item.get('TeacherNo')
           x = {'id':teacher_id,'name':teacher_name,'teacherno':teacher_no}
           teacher_list.append(x)       
      return teacher_list
    def getTeacher(self,id):
      query = teacherm()
      res = query.create_without_data(id)
      res.fetch()
      teachername = res.get('TeacherName')
      teacherno = res.get('TeacherNo')
      teacher = {'teacherid':id,'teachername':teachername,'teacherno':teacherno}
      return teacher
    def inserteacher(self,name,teacherno):
      teacher = teacherm()
      teacher.set('TeacherName',name)
      teacher.set('TeacherNo',teacherno)
      try:
          teacher.save()
          id = teacher.id
          GetApi.addUsertea(id,teacherno)
          return 210
      except Exception as e:
          print e.message
          return 211
    def modifyteacher(self,id,name,teacherno):
       updateteacher = teacherm.create_without_data(id)
       updateteacher.set('TeacherName',name)
       updateteacher.set('TeacherNo',teacherno)
       try:
           updateteacher.save()
           return 230
       except Exception as e:
           print e.message
           return 231
    def delteacher(self,id):
       delteacher = teacherm.create_without_data(id)
       try:
           GetApi.delUsertea(delteacher)
           delteacher.destroy()
           return 220
       except Exception as e:

           print e.message
           return 221
